/* vim: set sw=4 ts=4 sts=4 expandtab: */
#include <e.h>
#include <limits.h>
#include "config.h"
#include "e_mod_main.h"
#include "e_mod_oplist.h"

/* Local Function Prototypes */
static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name,
                                             const char *id, const char *style);
static void _gc_shutdown(E_Gadcon_Client *gcc);
static void _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
static char *_gc_label(E_Gadcon_Client_Class *client_class);
static const char *_gc_id_new(E_Gadcon_Client_Class *client_class);
static void _gc_id_del(E_Gadcon_Client_Class *client_class, const char *id);
static Evas_Object *_gc_icon(E_Gadcon_Client_Class *client_class, Evas *evas);

static void _oplist_conf_new(void);
static void _oplist_conf_free(void);
static int _oplist_conf_timer(void *data);
static Config_Item *_oplist_conf_item_get(const char *id);
static void _oplist_cb_mouse_down(void *data, Evas *evas, Evas_Object *obj,
                                             void *event);
static void _oplist_cb_menu_post(void *data, E_Menu *menu);
static void _oplist_cb_menu_configure(void *data, E_Menu *mn, E_Menu_Item *mi);

static E_Config_DD *conf_edd = NULL;
static E_Config_DD *conf_item_edd = NULL;
Config *oplist_conf = NULL;
static int uuid = 0;

static const E_Gadcon_Client_Class _gc_class = 
{
    GADCON_CLIENT_CLASS_VERSION, "Oplist", 
        {_gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, 
            _gc_id_new, _gc_id_del},
   E_GADCON_CLIENT_STYLE_PLAIN
};

EAPI E_Module_Api e_modapi = {E_MODULE_API_VERSION, "Oplist"};

/* Module Functions */
EAPI void *
e_modapi_init(E_Module *m) 
{
    char buf[PATH_MAX];

    /* Set up module's message catalogue */
    snprintf(buf, sizeof(buf), "%s/locale", e_module_dir_get(m));
    bindtextdomain(PACKAGE, buf);
    bind_textdomain_codeset(PACKAGE, "UTF-8");

    /* Location of theme to load for this module */
    snprintf(buf, sizeof(buf), "%s/e-module-oplist.edj", m->dir);

    /* Display this Modules config info in the main Config Panel */
    e_configure_registry_category_add("fileman", 80, D_("Files"), 
                                     NULL, "enlightenment/fileman");
    e_configure_registry_item_add("fileman/oplist", 80, D_("Oplist"), 
                                    NULL, buf, e_int_config_oplist_module);

    /* Define EET Data Storage */
    conf_item_edd = E_CONFIG_DD_NEW("Config_Item", Config_Item);
    #undef T
    #undef D
    #define T Config_Item
    #define D conf_item_edd
    E_CONFIG_VAL(D, T, id, STR);

    conf_edd = E_CONFIG_DD_NEW("Config", Config);
    #undef T
    #undef D
    #define T Config
    #define D conf_edd
    E_CONFIG_VAL(D, T, version, INT);
    E_CONFIG_VAL(D, T, hist_len, UCHAR);
    E_CONFIG_VAL(D, T, show_win, UCHAR);
    E_CONFIG_LIST(D, T, conf_items, conf_item_edd);

    /* Tell E to find any existing module data. First run ? */
    oplist_conf = e_config_domain_load("module.oplist", conf_edd);
    if (oplist_conf) 
    {
        if ((oplist_conf->version >> 16) < MOD_CONFIG_FILE_EPOCH) 
        {
            /* config too old */
            _oplist_conf_free();
            ecore_timer_add(1.0, _oplist_conf_timer,
			    "Oplist Module Configuration data needed "
			    "upgrading. Your old configuration<br> has been"
			    " wiped and a new set of defaults initialized. "
			    "This<br>will happen regularly during "
			    "development, so don't report a<br>bug. "
			    "This simply means the module needs "
			    "new configuration<br>data by default for "
			    "usable functionality that your old<br>"
			    "configuration simply lacks. This new set of "
			    "defaults will fix<br>that by adding it in. "
			    "You can re-configure things now to your<br>"
			    "liking. Sorry for the inconvenience.<br>");
        }
        /* Ardvarks */
        else if (oplist_conf->version > MOD_CONFIG_FILE_VERSION) 
        {
            /* config too new...wtf ? */
            _oplist_conf_free();
            ecore_timer_add(1.0, _oplist_conf_timer, 
			    "Your Oplist Module configuration is NEWER "
			    "than the module version. This is "
			    "very<br>strange. This should not happen unless"
			    " you downgraded<br>the module or "
			    "copied the configuration from a place where"
			    "<br>a newer version of the module "
			    "was running. This is bad and<br>as a "
			    "precaution your configuration has been now "
			    "restored to<br>defaults. Sorry for the "
			    "inconvenience.<br>");
        }
    }

    /* if we don't have a config yet, or it got erased above, 
     * then create a default one */
    if (!oplist_conf) _oplist_conf_new();

    oplist_conf->module = m;
    e_gadcon_provider_register(&_gc_class);
    //oplist_init();
   
    return m;
}


EAPI int 
e_modapi_shutdown(E_Module *m) 
{
    //oplist_shutdown();

    e_configure_registry_item_del("extensions/oplist");
    e_configure_registry_category_del("extensions");

    /* Kill the config dialog */
    if (oplist_conf->cfd) e_object_del(E_OBJECT(oplist_conf->cfd));
    oplist_conf->cfd = NULL;

    /* Tell E the module is now unloaded. Gets removed from shelves, etc. */
    oplist_conf->module = NULL;
    e_gadcon_provider_unregister(&_gc_class);

    _oplist_conf_free();

    E_CONFIG_DD_FREE(conf_item_edd);
    E_CONFIG_DD_FREE(conf_edd);
    return 1;
}

EAPI int 
e_modapi_save(E_Module *m) 
{
    e_config_domain_save("module.oplist", conf_edd, oplist_conf);
    return 1;
}

/* Gadcon Functions */
static E_Gadcon_Client *
_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style) 
{
    Instance *inst = NULL;

    inst = E_NEW(Instance, 1);
    inst->conf_item = _oplist_conf_item_get(id);

    inst->o_box = e_box_add(gc->evas);
    e_box_homogenous_set(inst->o_box, 0);
    e_box_orientation_set(inst->o_box, 0);
    e_box_align_set(inst->o_box, 0.0, 0.0);

    inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->o_box);
    inst->gcc->data = inst;

    evas_object_event_callback_add(inst->o_box, EVAS_CALLBACK_MOUSE_DOWN, 
                                                _oplist_cb_mouse_down, inst);

    instances = eina_list_append(instances, inst);
    oplist_fill_box(inst->o_box);

    return inst->gcc;
}

static void 
_gc_shutdown(E_Gadcon_Client *gcc) 
{
    Instance *inst = NULL;

    if (!(inst = gcc->data)) return;
    instances = eina_list_remove(instances, inst);

    if (inst->menu) 
    {
        e_menu_post_deactivate_callback_set(inst->menu, NULL, NULL);
        e_object_del(E_OBJECT(inst->menu));
        inst->menu = NULL;
    }

    if (inst->o_box) 
    {
        evas_object_event_callback_del(inst->o_box, EVAS_CALLBACK_MOUSE_DOWN, 
                                                        _oplist_cb_mouse_down);
        oplist_empty_box(inst->o_box);
        evas_object_del(inst->o_box);
    }

    E_FREE(inst);
}

static void 
_gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient) 
{
    Instance *inst;

    inst = gcc->data;
    switch (orient)
    {
        case E_GADCON_ORIENT_HORIZ:
        case E_GADCON_ORIENT_TOP:
        case E_GADCON_ORIENT_BOTTOM:
        case E_GADCON_ORIENT_CORNER_TL:
        case E_GADCON_ORIENT_CORNER_TR:
        case E_GADCON_ORIENT_CORNER_BL:
        case E_GADCON_ORIENT_CORNER_BR:
            /*
            e_gadcon_client_aspect_set(gcc, 100 * eina_list_count(volumes), 50);
            e_gadcon_client_min_size_set(gcc, 100 * eina_list_count(volumes), 50);
            */
            e_box_orientation_set(inst->o_box, 1);
            break;
        case E_GADCON_ORIENT_FLOAT:
        case E_GADCON_ORIENT_VERT:
        case E_GADCON_ORIENT_LEFT:
        case E_GADCON_ORIENT_RIGHT:
        case E_GADCON_ORIENT_CORNER_LT:
        case E_GADCON_ORIENT_CORNER_RT:
        case E_GADCON_ORIENT_CORNER_LB:
        case E_GADCON_ORIENT_CORNER_RB:
            /*
            e_gadcon_client_aspect_set(gcc, 200, 50 * eina_list_count(volumes)
                                                                         + 30);
            e_gadcon_client_min_size_set(gcc, 200, 50 * eina_list_count(volumes)
                                                                         + 30);
            */
            e_box_orientation_set(inst->o_box, 0);
            break;
        default:
            break;
    }

    oplist_fill_box(inst->o_box);
}

static char *
_gc_label(E_Gadcon_Client_Class *client_class) 
{
    return "Oplist";
}

static const char *
_gc_id_new(E_Gadcon_Client_Class *client_class) 
{
    Config_Item *ci = NULL;

    ci = _oplist_conf_item_get(NULL);
    return ci->id;
}

static void 
_gc_id_del(E_Gadcon_Client_Class *client_class, const char *id) 
{
    // Config_Item *ci = NULL;

    // if (!(ci = _places_conf_item_get(id))) return;

    // if (ci->id) eina_stringshare_del(ci->id);

    // places_conf->conf_items = eina_list_remove(places_conf->conf_items, ci);
    // E_FREE(ci);
}

static Evas_Object *
_gc_icon(E_Gadcon_Client_Class *client_class, Evas *evas) 
{
    Evas_Object *o = NULL;
    char buf[4096];

    snprintf(buf, sizeof(buf), "%s/e-module-oplist.edj",
                                                    oplist_conf->module->dir);
    o = edje_object_add(evas);
    edje_object_file_set(o, buf, "icon");
    return o;
}

/* Conf functions */
static void 
_oplist_conf_new(void) 
{
    Config_Item *ci = NULL;
    char buf[128];

    oplist_conf = E_NEW(Config, 1);
    oplist_conf->version = (MOD_CONFIG_FILE_EPOCH << 16);

#define IFMODCFG(v) if ((oplist_conf->version & 0xffff) < v) {
#define IFMODCFGEND }

    /* setup defaults */
    IFMODCFG(0x008d);
    oplist_conf->hist_len = 0;
    oplist_conf->show_win = 1;
    _places_conf_item_get(NULL);
    IFMODCFGEND;

    /* update the version */
    places_conf->version = MOD_CONFIG_FILE_VERSION;

    /* setup limits on the config properties here (if needed) */

    /* save the config to disk */
    e_config_save_queue();
}

static void 
_oplist_conf_free(void) 
{
    while (oplist_conf->conf_items) 
    {
        Config_Item *ci = NULL;

        ci = oplist_conf->conf_items->data;
        oplist_conf->conf_items = eina_list_remove_list(oplist_conf->conf_items,
                                            places_conf->conf_items);
        if (ci->id) eina_stringshare_del(ci->id);
        E_FREE(ci);
     }

   E_FREE(oplist_conf);
}

static int 
_oplist_conf_timer(void *data) 
{
    e_util_dialog_show("Oplist Configuration Updated", data);
    return 0;
}

static Config_Item *
_oplist_conf_item_get(const char *id) 
{
    Eina_List *l = NULL;
    Config_Item *ci = NULL;
    char buf[128];

    if (!id) 
    {
        /* nothing passed, return a new id */
        snprintf(buf, sizeof(buf), "%s.%d", _gc_class.name, ++uuid);
        id = buf;
    }
    else 
    {
        EINA_LIST_FOREACH(oplist_conf->conf_items, l, ci)
        {
            if (!ci)
                continue;

            if ((ci->id) && (!strcmp(ci->id, id)))
                return ci;
        }
    }

    ci = E_NEW(Config_Item, 1);
    ci->id = eina_stringshare_add(id);
    oplist_conf->conf_items = eina_list_append(oplist_conf->conf_items, ci);
    return ci;
}

static void 
_oplist_cb_mouse_down(void *data, Evas *evas, Evas_Object *obj, void *event) 
{
    Instance *inst;
    Evas_Event_Mouse_Down *ev;
    E_Zone *zone;
    E_Menu_Item *mi;
    int x, y;

    inst = data;
    if (!inst)
        return;

    ev = event;
    if (!ev)
       return;

    if ((ev->button == 3) && (!inst->menu)) 
    {
        /* grab current zone */
        zone = e_util_zone_current_get(e_manager_current_get());

        /* create popup menu */
        inst->menu = e_menu_new();
        e_menu_post_deactivate_callback_set(inst->menu, _oplist_cb_menu_post, 
                                            inst);

        mi = e_menu_item_new(inst->menu);
        e_menu_item_label_set(mi, D_("Configuration"));
        e_util_menu_item_edje_icon_set(mi, "enlightenment/configuration");
        e_menu_item_callback_set(mi, _oplist_cb_menu_configure, NULL);

        /* Each Gadget Client has a utility menu from the Container */
        e_gadcon_client_util_menu_items_append(inst->gcc, inst->menu, 0);
        e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon, &x, &y, 
                                          NULL, NULL);

        /* show the menu relative to gadgets position */
        e_menu_activate_mouse(inst->menu, zone, (x + ev->output.x), 
                                (y + ev->output.y), 1, 1, 
                                E_MENU_POP_DIRECTION_AUTO, ev->timestamp);
        evas_event_feed_mouse_up(inst->gcc->gadcon->evas, ev->button, 
                                EVAS_BUTTON_NONE, ev->timestamp, NULL);
    }
}

static void 
_oplist_cb_menu_post(void *data, E_Menu *menu) 
{
    Instance *inst;

    inst = menu;
    if (!inst)
        return;

    if (!inst->menu)
        return;

    e_object_del(E_OBJECT(inst->menu));
    inst->menu = NULL;
}

static void 
_oplist_cb_menu_configure(void *data, E_Menu *mn, E_Menu_Item *mi) 
{
    if (!oplist_conf) return;
    if (oplist_conf->cfd) return;
    e_int_config_oplist_module(mn->zone->container);
}
